package com.fileCenter.dao.impl;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.fileCenter.dao.BaseDAO;
import com.fileCenter.dao.FileDAO;
import com.fileCenter.exception.FileCenterException;
import com.fileCenter.pojo.File;

public class FileDAOImpl extends BaseDAO implements FileDAO {
	
	private static String queryByOwerIDHQL = "from File f where f.ownerId=:ownerId and f.status=:status";
	private static String getByIdsHQL = "from File f where f.id in (:ids) order by gmt_create desc";
	private static String updateStatusHQL = "update File f set f.status=:status where id in (:ids)";
	
	@SuppressWarnings("unchecked")
	@Override
	public List<File> queryByOwerId(long ownerId, int status) {
		if (ownerId <= 0 || status <= 0)
			return Collections.emptyList();
		return getHibernateTemplate().findByNamedParam(queryByOwerIDHQL, 
				new String[]{"ownerId","status"}, new Object[]{ownerId, status});
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<File> getByIds(List<Long> ids) {
		if (CollectionUtils.isEmpty(ids))
			return Collections.emptyList();
		
		final List<Long> localIds = ids;
		List<File> list = (List<File>) getHibernateTemplate().execute(
		new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query = session.createQuery(getByIdsHQL);
				query.setParameterList("ids", localIds);
				return query.list();
			}
		});
		return (List<File>) (CollectionUtils.isEmpty(list) ? 
				Collections.emptyList() : list);
	}

	@Override
	public void insert(File file) {
		if (file == null) {
			throw new FileCenterException("param error!");
		}
		getHibernateTemplate().save(file);
	}

	@Override
	public void updateStatus(List<Long> fileIds, int status) {
		if (CollectionUtils.isEmpty(fileIds) || status <= 0) {
			throw new FileCenterException("param error!");
		}
		final List<Long> localIds = fileIds;
		final int localStatus = status;
		getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query = session.createQuery(updateStatusHQL);
				query.setParameterList("ids", localIds);
				query.setParameter("status", localStatus);
				return query.executeUpdate();
			}
		});
	}

	@Override
	public File getById(Long fileId) {
		if (fileId <= 0) {
			return null;
		}
		return (File) getHibernateTemplate().get(File.class, fileId);
	}
	
	@Override
	public void delete(File file) {
		if (file == null || file.getId() <= 0) {
			throw new FileCenterException("param error!");
		}
		getHibernateTemplate().delete(file);
	}

}
